{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T13:25:31.478412Z",
     "start_time": "2025-01-07T13:25:31.140131Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:26:08.793878Z",
     "start_time": "2025-01-07T13:26:08.787112Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建一个DataFrame对象  列表套字典\n",
    "dict_data = {'A': 1,\n",
    "             'B': pd.Timestamp('20190926'),\n",
    "             'C': pd.Series(1, index=list(range(4)),dtype='float32'),\n",
    "             'D': np.array([1,2,3,4],dtype='int32'),\n",
    "             'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "             'F': 'wangdao' }\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2.index)"
   ],
   "id": "16fb4847a4792fbd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index([0, 1, 2, 3], dtype='int64')\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:26:42.817184Z",
     "start_time": "2025-01-07T13:26:42.810429Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj = pd.Series(range(5), index = list(\"abcde\"))# 创建一个Series对象\n",
    "print(ser_obj)\n",
    "ser_obj.index# 查看索引"
   ],
   "id": "ff4d14efb3dd29b8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:26:53.116089Z",
     "start_time": "2025-01-07T13:26:53.112824Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 行索引，不仅可以用索引名，可以用索引位置或来取\n",
    "print(ser_obj['b']) #索引名\n",
    "print(ser_obj[2]) #位置索引"
   ],
   "id": "546817b0ddc9ff33",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\MECHREV\\AppData\\Local\\Temp\\ipykernel_26588\\2834633063.py:3: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  print(ser_obj[2]) #位置索引\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:27:02.846167Z",
     "start_time": "2025-01-07T13:27:02.842301Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj.loc['b']) #索引名\n",
    "print(ser_obj.iloc[2]) #位置索引"
   ],
   "id": "b9d28380889d1ce9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:27:19.612238Z",
     "start_time": "2025-01-07T13:27:19.606533Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 切片索引\n",
    "print(ser_obj.iloc[1:3])  #索引位置取数据，左闭右开\n",
    "print(ser_obj.loc['b':'d'])  #记住索引名  左闭右闭"
   ],
   "id": "a1e7acd27f20370e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:27:26.352730Z",
     "start_time": "2025-01-07T13:27:26.344484Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不连续索引\n",
    "print(ser_obj.iloc[[0, 2, 4]])\n",
    "print(ser_obj.loc[['a', 'e']])"
   ],
   "id": "469f00884e2459cb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "c    2\n",
      "e    4\n",
      "dtype: int64\n",
      "a    0\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:34:23.224967Z",
     "start_time": "2025-01-07T13:34:23.219476Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引\n",
    "ser_bool = ser_obj > 2 \n",
    "print(ser_obj)\n",
    "print(ser_bool)\n"
   ],
   "id": "2c2e6118e378dfed",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:34:40.407448Z",
     "start_time": "2025-01-07T13:34:40.402769Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('-'*50)\n",
    "print(ser_obj[ser_bool])\n",
    "\n",
    "print(ser_obj[ser_obj > 2]) #取出大于2的元素"
   ],
   "id": "59779ac7a950041e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.4 DataFrame索引",
   "id": "72717bcd32a56ee1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:35:59.709334Z",
     "start_time": "2025-01-07T13:35:59.704647Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),\n",
    "                      columns = ['a', 'b', 'c', 'd'])\n",
    "print(df_obj.head())"
   ],
   "id": "e103999eda0c9136",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0 -2.654707 -1.441749  0.610438 -0.553681\n",
      "1  0.785341 -0.235398 -0.945980  0.101021\n",
      "2 -0.103832 -1.196533 -1.580997 -0.870125\n",
      "3  1.855252  0.869722  0.487336  0.201044\n",
      "4 -0.413238 -0.971833 -0.734388  0.143556\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:39:00.937372Z",
     "start_time": "2025-01-07T13:39:00.920473Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 列索引\n",
    "print(df_obj['a']) # 返回Series类型\n",
    "print('-'*50)\n",
    "print(df_obj[['a']]) # 返回DataFrame类型\n",
    "print('-'*50)\n",
    "print(type(df_obj[['a']])) # 返回DataFrame类型"
   ],
   "id": "7d2b61469e8556e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -2.654707\n",
      "1    0.785341\n",
      "2   -0.103832\n",
      "3    1.855252\n",
      "4   -0.413238\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "          a\n",
      "0 -2.654707\n",
      "1  0.785341\n",
      "2 -0.103832\n",
      "3  1.855252\n",
      "4 -0.413238\n",
      "--------------------------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:39:13.696581Z",
     "start_time": "2025-01-07T13:39:13.691708Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标签索引 loc，建议使用loc，效率更高\n",
    "# Series\n",
    "print(ser_obj)\n",
    "print(ser_obj['b':'d'])\n",
    "print(ser_obj.loc['b':'d']) #前闭后闭\n",
    "print('-'*50)\n"
   ],
   "id": "f43dd33e509cb5f8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:39:30.200540Z",
     "start_time": "2025-01-07T13:39:30.194925Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),# 5行4列\n",
    "                      columns = list('abcd'),# 列名\n",
    "                      index=list('abcde'))# 行名\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj['a'])  #建议不用,拿的是列\n",
    "print('-'*50)\n",
    "print(df_obj.loc['a'])  #拿的是行\n",
    "print('-'*50)"
   ],
   "id": "3233741a33c9a3fb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a  0.612364  1.207125 -0.177961  1.051736\n",
      "b -0.596849  0.638970 -0.487777 -0.815223\n",
      "c -0.907165 -1.510556  0.179715  0.116346\n",
      "d -0.989325 -1.198764  0.331216 -0.555309\n",
      "e -1.997113  0.459350 -0.009699  0.608282\n",
      "--------------------------------------------------\n",
      "a    0.612364\n",
      "b   -0.596849\n",
      "c   -0.907165\n",
      "d   -0.989325\n",
      "e   -1.997113\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n",
      "a    0.612364\n",
      "b    1.207125\n",
      "c   -0.177961\n",
      "d    1.051736\n",
      "Name: a, dtype: float64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:40:05.808384Z",
     "start_time": "2025-01-07T13:40:05.801465Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一个参数索引行，第二个参数是列,loc或者iloc效率高于直接用取下标的方式，前闭后闭\n",
    "print(df_obj.loc['a':'c', 'b':'d']) #连续索引\n",
    "print('-'*50)\n",
    "print(df_obj.loc[['a','c'], ['b','d']]) #不连续索引\n",
    "print('-'*50)\n",
    "print(df_obj.loc[['c'],['b']]) #取一个值,返回的是DataFrame类型\n",
    "print('-'*50)\n",
    "print(df_obj.loc['c','b'])  #取一个值"
   ],
   "id": "e38dd05ccb8c82a8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          b         c         d\n",
      "a  1.207125 -0.177961  1.051736\n",
      "b  0.638970 -0.487777 -0.815223\n",
      "c -1.510556  0.179715  0.116346\n",
      "--------------------------------------------------\n",
      "          b         d\n",
      "a  1.207125  1.051736\n",
      "c -1.510556  0.116346\n",
      "--------------------------------------------------\n",
      "          b\n",
      "c -1.510556\n",
      "--------------------------------------------------\n",
      "-1.5105563323629803\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:40:14.961073Z",
     "start_time": "2025-01-07T13:40:14.957185Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj\n",
    "print('-'*50)\n",
    "# Series\n",
    "print(ser_obj[1:3])\n",
    "print('-'*50)\n",
    "print(ser_obj.iloc[1:3]) # 前闭后开[)，效率高"
   ],
   "id": "17de92b1829786a6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:40:51.590448Z",
     "start_time": "2025-01-07T13:40:51.584008Z"
    }
   },
   "cell_type": "code",
   "source": "df_obj",
   "id": "6c5732573c3dba49",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          a         b         c         d\n",
       "a  0.612364  1.207125 -0.177961  1.051736\n",
       "b -0.596849  0.638970 -0.487777 -0.815223\n",
       "c -0.907165 -1.510556  0.179715  0.116346\n",
       "d -0.989325 -1.198764  0.331216 -0.555309\n",
       "e -1.997113  0.459350 -0.009699  0.608282"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.612364</td>\n",
       "      <td>1.207125</td>\n",
       "      <td>-0.177961</td>\n",
       "      <td>1.051736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>-0.596849</td>\n",
       "      <td>0.638970</td>\n",
       "      <td>-0.487777</td>\n",
       "      <td>-0.815223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>-0.907165</td>\n",
       "      <td>-1.510556</td>\n",
       "      <td>0.179715</td>\n",
       "      <td>0.116346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>-0.989325</td>\n",
       "      <td>-1.198764</td>\n",
       "      <td>0.331216</td>\n",
       "      <td>-0.555309</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>-1.997113</td>\n",
       "      <td>0.459350</td>\n",
       "      <td>-0.009699</td>\n",
       "      <td>0.608282</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:41:27.624221Z",
     "start_time": "2025-01-07T13:41:27.617723Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame，iloc是前闭后开[)\n",
    "print(df_obj)\n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0:2, 0:2]) \n",
    "print('-'*50)\n",
    "print(df_obj.iloc[[0,2], [0,2]]) # 不连续索引\n",
    "print('-'*50)\n",
    "print(df_obj.iloc[0,0]) # 取一个值"
   ],
   "id": "74afa24ed33b8681",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "a  0.612364  1.207125 -0.177961  1.051736\n",
      "b -0.596849  0.638970 -0.487777 -0.815223\n",
      "c -0.907165 -1.510556  0.179715  0.116346\n",
      "d -0.989325 -1.198764  0.331216 -0.555309\n",
      "e -1.997113  0.459350 -0.009699  0.608282\n",
      "--------------------------------------------------\n",
      "          a         b\n",
      "a  0.612364  1.207125\n",
      "b -0.596849  0.638970\n",
      "--------------------------------------------------\n",
      "          a         c\n",
      "a  0.612364 -0.177961\n",
      "c -0.907165  0.179715\n",
      "--------------------------------------------------\n",
      "0.6123638383155307\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:41:35.434353Z",
     "start_time": "2025-01-07T13:41:35.428454Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#没有设置行和列索引的DataFrame，iloc和loc的区别\n",
    "df_obj2 = pd.DataFrame(np.random.randn(5,4))\n",
    "print(df_obj2)\n",
    "print('-'*50)\n",
    "print(df_obj2.iloc[0:2]) #左闭右开 2行\n",
    "print('-'*50)\n",
    "print(df_obj2.loc[0:2]) #左闭右闭 3行"
   ],
   "id": "d3d03da0419324f8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0  0.646516  0.397275  1.128966  1.046077\n",
      "1  1.662208  0.303005  1.203630 -1.264651\n",
      "2  0.425168  0.473019 -2.014920 -0.397718\n",
      "3  1.060525  0.319414  0.696399  1.975795\n",
      "4 -0.762794  0.496685  2.656744  0.075672\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.646516  0.397275  1.128966  1.046077\n",
      "1  1.662208  0.303005  1.203630 -1.264651\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.646516  0.397275  1.128966  1.046077\n",
      "1  1.662208  0.303005  1.203630 -1.264651\n",
      "2  0.425168  0.473019 -2.014920 -0.397718\n"
     ]
    }
   ],
   "execution_count": 20
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
